package com.service;

import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.TRX_OUT;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetPrdInDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_wo;
import com.tx.Xpinqmdl.XpinqmdlI;
import com.tx.Xpinqmdl.XpinqmdlO;
import com.tx.Xpinqmdl.XpinqmdlOA;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqmdlService")
public class XpinqmdlService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpinqmdlService.class);
	private static String TX_NAME = "XPINQMDL";
	@Autowired
	private XpinqmdlI inTrx;
	@Autowired
	private XpinqmdlO outTrx;
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	
	@Autowired
	private IRetPrdInDao retPrdInDao;
	private String gszMsgBuf = "";

	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		
		MqOpe mqOpe = new MqOpe();
		String reply ="";
		gszMsgBuf = "";
		
		inTrx = new XpinqmdlI();
		outTrx = new XpinqmdlO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);
		try{
			inTrx = (XpinqmdlI) mqOpe.inStr2Obj(strInMsg,inTrx);
			String actionFlg = inTrx.getAction_flg();
			if(actionFlg.equals("Q")){
				queryMDLWoInfo();
			}
			if( RETURN_CODE_OK.equals(outTrx.getRtn_code()) ){
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex){
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
		}finally{
			reply = mqOpe.obj2Str(outTrx);
			logger.info(reply);
		}
		return reply;
	}

	private void queryMDLWoInfo() {
		// TODO Auto-generated method stub
		String mdl_id="";
		Timestamp from_date,to_date;
		List<XpinqmdlOA> oaryList = new ArrayList<XpinqmdlOA>();
		mdl_id = inTrx.getMdl_id();
		from_date = inTrx.getFrom_date();
		to_date = inTrx.getTo_date();
		String hql = "FROM Ret_wo where (mdl_id_fk='"+mdl_id+"' OR fm_mdl_id_fk='"+mdl_id+"') AND WO_TYP='W' AND crt_timestamp>='"+
				from_date+"'AND crt_timestamp<='"+to_date+"'";
		List<Ret_wo>retSoList = retWODao.find(hql);
		if(retSoList.size() !=0){
			for(Ret_wo ret_so : retSoList){
//				String sql_wo = "FROM Ret_wo where so_id='"+ret_so.getWo_id()+"' AND WO_TYP='W'";
//				List<Ret_wo>retWoList = retWODao.find(sql_wo);
//				if(retWoList.size()!=0){
//					for(Ret_wo ret_wo : retWoList ){
						XpinqmdlOA oary = new XpinqmdlOA();
//						Timestamp crt_timestamp = ret_so.getCrt_timestamp();
						oary.setSo_id(ret_so.getSo_id());
						oary.setCrt_timestamp(ret_so.getCrt_timestamp());
						oary.setWo_id(ret_so.getWo_id());
						oary.setRl_mtrl_date(ret_so.getPln_stb_timestamp());
						oary.setRl_mtrl_qty(ret_so.getPln_prd_qty());
						oary.setCmp_prd_qty(ret_so.getComp_prd_qty().intValue()+ ret_so.getWh_in_prd_qty()+ret_so.getWh_out_prd_qty()+ret_so.getWip_wh_in_prd_qty());
						oary.setWip_qty(ret_so.getRl_prd_qty()-oary.getCmp_prd_qty());
						oary.setNo_rl_prd_qty(ret_so.getPln_prd_qty()-ret_so.getRl_prd_qty());
						oary.setWh_out_prd_qty(ret_so.getWh_out_prd_qty());
						oary.setNo_out_prd_qty(ret_so.getPln_prd_qty()-ret_so.getWh_out_prd_qty());
						/**
						 * 实际来料
						 */
//						String sql_mtrl = "SELECT SUM(PRD_TL_QTY) MTRL_TL_QTY,RCV_TIMESTAMP "
//										+"FROM RET_PRD_IN where WO_ID_FK='"+ret_so.getWo_id()+"' ORDER BY RCV_TIMESTAMP";
//						List<Object[]> objList_Mtrl = retPrdInDao.findBySQL(sql_mtrl);
//						
//						if(objList_Mtrl.size()>0){
//							Object mtrlInfo[] = objList_Mtrl.get(0);
//							if(mtrlInfo[1]!=null){
//								oary.setRl_mtrl_date((Timestamp) mtrlInfo[1]);//实际来料时间
//							}
//							if(mtrlInfo[0]!=null){
//								oary.setRl_mtrl_qty(mtrlInfo[0].toString());//实际来料数量
//							}
//						}
						oaryList.add(oary);
//					}
//				}
			}
		}
		outTrx.setOary(oaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
		outTrx.setTbl_cnt(oaryList.size());
	}
}
